import os
from do_get_aminer_data import *
from urllib import request,parse
import requests
from http import cookiejar
from urllib.error import URLError,HTTPError


def auth_proxy(openner,request,FAILED_CODE):
    try:
        response = openner.open(request,timeout = 3)
    except HTTPError as err:
        return err.code
    except URLError as err:
        return FAILED_CODE
    else:
        return response.getcode()



def main(university_name):

    with open("./after_delete_repeat_univeristy_sex_info_list_copy/" + university_name + ".txt",mode = 'r',encoding = 'utf-8') as f:
        lines = f.read().splitlines()
    f.close()
    list = []
    with open("./after_delete_repeat_univeristy_sex_info_list_copy/" + university_name + ".txt",mode = 'a+',encoding = 'utf-8') as f_w:
        for line in lines:
            list.append(line)
            # print(line)
            if str({'AuN': 'chengwei hao', 'AfN': 'anhui university'}) == line:
                print(1111)
            #     continue
            # f_w.write(line)
    # print(list)
    f_w.close()

if __name__ == '__main__':
    # main("anhui university")
    FAILED_CODE = 666
    good_proxy = []
    proxy_list = [
        {"https": "112.85.174.116:9999"},
        {"https":"112.85.166.237:9999"},
        {'https':'112.85.131.147:9999'},
        {'https':'112.85.149.183:9999'}
    ]

    req = request.Request("https://api.aminer.cn/api/kit/gender/api/ch?name=dachuang+li&aff=anhui+university")
    for proxy in proxy_list:
        proxy_handler = request.ProxyHandler(proxy)
        openner = request.build_opener(proxy_handler)

        num = auth_proxy(openner,req,FAILED_CODE)
        print(num)
        if num == 200:
            good_proxy.append(proxy)
    print("可代理的为:",good_proxy)
